package com.kylin.callable;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class CallableTest {

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        //new Thread(new MyThread()).start();


        //new Thread(new Runnable()).start;
        //new Thread(new FutureTask<V>()).start;
        //new Thread(new FutureTask<V>(Callable)).start;
        new Thread().start();

        MyThread thread = new MyThread();
        FutureTask<String> futureTask = new FutureTask<>(thread);//适配类

        new Thread(futureTask,"A").start();
        new Thread(futureTask,"B").start();//结果会被缓存，从而提供效率

        String s = futureTask.get();//获取Callable的返回结果 可能会产生阻塞
        System.out.println(s);
    }
}

class MyThread implements Callable<String>{

    @Override
    public String call() throws Exception {
        System.out.println("call()");
        //耗时
        return "123456";
    }
}


//class MyThread implements Runnable{
//
//    @Override
//    public void run() {
//
//    }
//}


